// extract module
var fs = require('fs');

// create server
var server = require('http').createServer();
var io = require('socket.io').listen(server);

// run server
server.listen(52273, function() {
	console.log('Server running at http://127.0.0.1:52273');
});

// web server event handlers
server.on('request', function(request, response) {
	fs.readFile('resources/client.html', 'utf8', function(error, data) {
		response.writeHead(200, {'Content-Type': 'text/html'});
		response.end(data);
	});
});

// socket server event handlers
io.sockets.on('connection', function(socket) {
	// setname event handler
	socket.on('setname', function(name) {
		// store client data
		socket.set('name', name);
	});
	
	// getname event handler
	socket.on('getname', function() {
		// get data
		socket.get('name', function(error, name) {
			// send data
			socket.emit('responsename', name);
		});
	});
});